﻿using System;
using System.Windows.Forms;
using System.Xml;

namespace SPDevExplorer
{
    public partial class SiteExplorer
    {
        private void mnuDeleteFolder_Click(object sender, EventArgs e)
        {
            if (_ctlExplorerTree.SelectedNode != null)
            {
                DeleteFolder(_ctlExplorerTree.SelectedNode);
            }
        }

        private void mnuGetFolderFiles_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult res = MessageBox.Show("Overwrite existing files?", "Overwrite files",
                                                   MessageBoxButtons.YesNo);
                EnsureFilesInFolder(_ctlExplorerTree.SelectedNode, (res == DialogResult.Yes));
            }
            catch (Exception ex)
            {
                LogEvent(ex);
            }
        }

        private void mnuGetFolderFilesRecursive_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult res = MessageBox.Show("Overwrite existing files?", "Overwrite files",
                                                   MessageBoxButtons.YesNo);
                RecurseEnsureFiles(_ctlExplorerTree.SelectedNode, (res == DialogResult.Yes));
            }
            catch (Exception ex)
            {
                LogEvent(ex, false);
                MessageBox.Show("Failed to retrieve all files");
            }
        }

        private void mnuFolderInfo_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode selectedFolder = _ctlExplorerTree.SelectedNode;
                FolderProperties fp = new FolderProperties(selectedFolder);
                MessageBox.Show(fp.Xml.OuterXml, String.Format("Information on folder '{0}'", fp.Name),
                                MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                LogEvent("Can't get information on the selected folder", ex);
            }
        }

        private void mnuRenameFolder_Click(object sender, EventArgs e)
        {
            _ctlExplorerTree.SelectedNode.BeginEdit();
        }

        private void mnuAddFiles_Click(object sender, EventArgs e)
        {
            TreeNode folderNode = _ctlExplorerTree.SelectedNode;

            SPDevExplorer.DlgAddFiles addFilesDialog = new SPDevExplorer.DlgAddFiles();
            ListView files = addFilesDialog.ListOfFiles;
            addFilesDialog.AddFilesToListView(folderNode);
            DialogResult result = addFilesDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                FolderProperties fp = new FolderProperties(folderNode);

                // Add selected files to SharePoint
                foreach (ListViewItem fileItem in files.Items)
                {
                    if (fileItem.Checked)
                    {
                        string fileName = fileItem.Text;

                        // Create a new empty file at SharePoint side
                        TreeNode newFileNode = CreateFile(folderNode, fileName, fp);

                        // Now save the contents of the file in the working folder to SharePoint
                        SaveFileFromWorkingFolderToSharePoint(newFileNode);
                    }
                }
            }
        }

        private void mnuNewFolder_Click(object sender, EventArgs e)
        {
            try
            {
                FolderProperties fp = new FolderProperties(_ctlExplorerTree.SelectedNode);
                TreeNode oNewNode = new TreeNode("New Folder", (int) NodeImages.Folder, (int) NodeImages.Folder);
                _ctlExplorerTree.SelectedNode.Nodes.Add(oNewNode);
                string sXml = DevServices(_ctlExplorerTree.SelectedNode).NewFolder(fp.ID, "New Folder");
                XmlDocument oDoc = new XmlDocument();
                oDoc.LoadXml(sXml);

                XmlElement oImported = (XmlElement) fp.Xml.OwnerDocument.ImportNode(oDoc.DocumentElement, true);
                fp.Xml.AppendChild(oImported);

                oNewNode.Tag = oImported;
                _ctlExplorerTree.SelectedNode = oNewNode;

                oNewNode.BeginEdit();
            }
            catch (Exception ex)
            {
                LogEvent(ex);
            }
        }

        private void mnuRefreshFolder_Click(object sender, EventArgs e)
        {
            try
            {
                RefreshFolder(_ctlExplorerTree.SelectedNode);
            }
            catch (Exception ex)
            {
                LogEvent(ex);
            }
        }
    }
}